package com.zb.jnlxc.dao;

import java.util.*;

import org.hibernate.Query;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import com.ZLHW.base.Exception.BaseErrorModel;
import com.ZLHW.base.dao.DAO;
import com.zb.jnlxc.constants.ProcessConstants;
import com.zb.jnlxc.model.OrderDetail;
import com.zb.jnlxc.model.ProductProcess;

/**
 * Created by Administrator on 2014/9/29.
 */
@Component
public class ProductProcessDAO extends DAO<ProductProcess, Integer> {
    Logger logger = LoggerFactory.getLogger(ProductProcess.class);

    public boolean liaoKuangUnUsed(Integer liaoKuangId) {
        String hql = "from ProductProcess t where t.liaoKuang.dbId=? and t.finished=?";
        List<ProductProcess> list = findByHQL(hql, liaoKuangId, false);
        return CollectionUtils.isEmpty(list);
    }

    public List<ProductProcess> getByLiaoKuangId(Integer liaoKuangId) {
        String hql = "from ProductProcess t where t.liaoKuang.dbId=? and t.finished=?";
        return findByHQL(hql, liaoKuangId, false);
    }

    public float getTheoryWeight(Integer yangHuaId) {
        ProductProcess productProcess = getById(yangHuaId);
        Integer paichanSize = productProcess.getProductDetail().getPaichanOrderDetail().getPaichanSize();
        float kpm = productProcess.getProductDetail().getPaichanOrderDetail().getOrderDetail()
            .getOrderForm().getScheme().getKpm();
        float weight = (productProcess.getUncompQuantity() * paichanSize * kpm) / 1000000;
        return weight;
    }

    public float getDeliveryaWeight(Integer deliveryId) {
        ProductProcess productProcess = getById(deliveryId);
        Integer setSize = productProcess.getProductDetail().getPaichanOrderDetail().getSetSize();
        float kpm = productProcess.getProductDetail().getPaichanOrderDetail().getOrderDetail()
                .getOrderForm().getScheme().getKpm();
        float weight = (productProcess.getUncompQuantity() * setSize * kpm) / 1000000;
        return weight;
    }

    public ProductProcess getByLassProcess(Integer processId) {
        String hql = "from ProductProcess t where t.lastProcessId=?  and t.finished=? ";
        return findOneByHQL(hql, processId, false);
    }

    public void updateLastProcess(ProductProcess currentProcess, ProductProcess lastProcess) {
        int finishQuantity = currentProcess.getDefectiveQuantity() + currentProcess.getQuantity();
        if (finishQuantity > lastProcess.getUncompQuantity()) {
            throw new BaseErrorModel("数量超出，本次最多处理" + lastProcess.getUncompQuantity() + "个", "");
        }
        lastProcess.setCompQuantity(lastProcess.getCompQuantity() + finishQuantity);
        lastProcess.setUncompQuantity(lastProcess.getUncompQuantity() - finishQuantity);
        if (lastProcess.getUncompQuantity() == 0) {
            lastProcess.setFinished(true);
        }
        update(lastProcess);
    }

    /**
     * 判断排产明细是否可以结束
     * 处于入库与挤压中间流程，均不能人工判定完成
     * @return
     */
    public boolean canFinishPaiChanDetial(Integer panChanId) {
        String hql = "from ProductProcess t where t.productDetail.paichanOrderDetail.dbId=:dbId and t.processName not in (:processNames) and t.finished=false";
        Query query = getSession().createQuery(hql);
        query.setParameter("dbId", panChanId);
        List<String> processNames = new ArrayList<String>();
        processNames.add(ProcessConstants.DELIVER);
        query.setParameterList("processNames", processNames);
        List<ProductProcess> productProcesses = query.list();
        return CollectionUtils.isEmpty(productProcesses);
    }

    /**
     * 判断订单是否可以结束
     * 处于入库与挤压中间流程，均不能人工判定完成
     * @return
     */
    public boolean canFinishOrder(Integer orderFormId) {
        String hql = "from ProductProcess t where t.productDetail.paichanOrderDetail.orderDetail.orderForm.dbId=:dbId and t.processName not in (:processNames) and t.finished=false";
        Query query = getSession().createQuery(hql);
        query.setParameter("dbId", orderFormId);
        List<String> processNames = new ArrayList<String>();
        processNames.add(ProcessConstants.DELIVER);
        query.setParameterList("processNames", processNames);
        List<ProductProcess> productProcesses = query.list();
        return CollectionUtils.isEmpty(productProcesses);
    }

    public Map<String,Integer> getTotalByOrderDetail(OrderDetail orderDetail) {
        String hql = " from ProductProcess t where t.productDetail.paichanOrderDetail.orderDetail=? and processName=?";
        List<ProductProcess> productProcesses = findByHQL(hql, orderDetail,
            ProcessConstants.STORAGE);
        int totalQuantity = 0;
        int totalWeight = 0;
        for (ProductProcess productProcess : productProcesses) {
            totalQuantity += productProcess.getQuantity();
            if(productProcess.getRealWeight()!=null){
                totalWeight += productProcess.getRealWeight();
            }
        }
        Map<String,Integer> map = new HashMap<String, Integer>();
        map.put("totalQuantity",totalQuantity);
        map.put("totalWeight",totalWeight);
        return map;
    }

}
